package com.wasp.collector.utils;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;
import java.util.UUID;  
  
public class FileOperator {  
      
    /** 
     * 创建文件 
     * @throws IOException  
     */  
    public static void createFile(String content ,String fileFullPath,String folder) throws IOException { 
    	File folderFile = new File(folder);
    	if(!folderFile.exists()){
    		boolean m = folderFile.mkdirs();
    		System.out.println(m);
    	}
        Path target =  FileSystems.getDefault().getPath(fileFullPath);
        Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rw-rw-rw-");  
        FileAttribute<Set<PosixFilePermission>> attrs = PosixFilePermissions.asFileAttribute(perms);
        int lastIndex = fileFullPath.lastIndexOf(".");
    	String fileType= fileFullPath.substring(lastIndex+1,fileFullPath.length());
    	String fileName= fileFullPath.substring(0,lastIndex)+"__"+UUID.randomUUID().toString();
    	fileFullPath = fileName+"."+fileType;
    	target =  FileSystems.getDefault().getPath(fileFullPath);
    	Files.createFile(target);
        OpenOption options = StandardOpenOption.WRITE;
        Files.write(target, content.getBytes(StandardCharsets.UTF_8),
        		options);
    }  
      
    /** 
     * 删除文件 
     * @throws IOException  
     */  
    public static void deleteFile(String path) throws IOException {  
        Path target = Paths.get(path);  
        Files.delete(target);         
    }  
      
   /* *//** 
     * 拷贝文件 
     * @throws IOException  
     *//*  
    public static void copyFile() throws IOException {  
        Path source = Paths.get("F:/study-copy.txt");  
        Path target = Paths.get("F:/study-copy2.txt");  
        Files.copy(source, target, REPLACE_EXISTING);         
    }  
      
    *//** 
     * 移动文件 
     * @throws IOException  
     *//*  
    public static void moveFile() throws IOException {  
        Path source = Paths.get("F:/study-copy.txt");  
        Path target = Paths.get("d:/study-copy.txt");  
          
        Files.move(source, target, REPLACE_EXISTING, COPY_ATTRIBUTES);        
    }  
      
    public static void main(String[] args) {  
        try {  
            long start = System.currentTimeMillis();  
            //createFile();  
            //deleteFile();  
            //copyFile();  
            moveFile();  
              
            long end = System.currentTimeMillis();  
            System.out.println("consume -> " + (end - start));  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  */
}  